home *** CD-ROM | disk | FTP | other *** search
/ Aminet 22 / Aminet 22 (1997)(GTI - Schatztruhe)[!][Dec 1997].iso / Tools / ClassAct / Examples / String / stringexample.c < prev    next >
C/C++ Source or Header  |  1997-04-27  |  7KB  |  319 lines

  1. ;/* String Example
  2. sc link stringexample.c lib lib:classact.lib
  3. quit
  4. */
  5.  
  6. /**
  7.  **  StringExample.c -- String class Example.
  8.  **
  9.  **  This is a simple example testing some of the capabilities of the
  10.  **  String gadget class.
  11.  **
  12.  **  This code opens a window and then creates 2 String gadgets which
  13.  **  are subsequently attached to the window's gadget list.  One uses
  14.  **  and edit hook, and the other does not.  Notice that you can tab
  15.  **  cycle between them.
  16.  **/
  17.  
  18. /* system includes
  19.  */
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23.  
  24. #include <exec/types.h>
  25. #include <exec/memory.h>
  26. #include <intuition/intuition.h>
  27. #include <intuition/gadgetclass.h>
  28. #include <intuition/sghooks.h>    /* required for string hooks */
  29. #include <graphics/gfxbase.h>
  30. #include <graphics/text.h>
  31. #include <graphics/gfxmacros.h>
  32. #include <utility/tagitem.h>
  33. #include <workbench/startup.h>
  34. #include <workbench/workbench.h>
  35.  
  36. #include <proto/intuition.h>
  37. #include <proto/graphics.h>
  38. #include <proto/exec.h>
  39. #include <proto/dos.h>
  40. #include <proto/utility.h>
  41. #include <proto/wb.h>
  42. #include <proto/icon.h>
  43.  
  44. /* ClassAct includes
  45.  */
  46. #include <classact.h>
  47.  
  48.  
  49. enum
  50. {
  51.     GID_MAIN=0,
  52.     GID_STRING1,
  53.     GID_STRING2,
  54.     GID_DOWN,
  55.     GID_UP,
  56.     GID_QUIT,
  57.     GID_LAST
  58. };
  59.  
  60. enum
  61. {
  62.     WID_MAIN=0,
  63.     WID_LAST
  64. };
  65.  
  66. enum
  67. {
  68.     OID_MAIN=0,
  69.     OID_LAST
  70. };
  71.  
  72.  
  73. /* hook function typedef
  74.  */
  75. typedef ULONG (*HookFunction)(VOID);
  76.  
  77. /* hook function prototype
  78.  */
  79. ULONG __saveds __asm PasswordHook(
  80.     register __a0 struct Hook *hook,
  81.     register __a2 struct SGWork *sgw,
  82.     register __a1 ULONG *msg);
  83.  
  84. #define SMAX 24
  85.  
  86. #define PASSWORDCHAR '*'
  87.  
  88. UBYTE initialstring[] = "Testing";
  89.  
  90. int main(void)
  91. {
  92.     struct MsgPort *AppPort;
  93.  
  94.     struct Window *windows[WID_LAST];
  95.  
  96.     struct Gadget *gadgets[GID_LAST];
  97.  
  98.     Object *objects[OID_LAST];
  99.  
  100.     /* make sure our classes opened... */
  101.     if (!ButtonBase || !StringBase || !WindowBase || !LayoutBase)
  102.         return(30);
  103.     else if ( AppPort = CreateMsgPort() )
  104.     {
  105.         struct Hook edithook1;
  106.         STRPTR hookdata1;
  107.  
  108.         /* The password edit hook needs special care, we need to look at
  109.          * edithook.h_Data to set/get the real password text. Additionally,
  110.          * we need to Alloc/Free maxchars bytes for its buffer!
  111.          */
  112.         hookdata1 = (STRPTR)AllocVec( (SMAX + 2), MEMF_ANY | MEMF_CLEAR);
  113.  
  114.         if (hookdata1)
  115.         {
  116.             CA_SetUpHook(edithook1, PasswordHook, (STRPTR)hookdata1);
  117.  
  118.             /* copy real string data into the hidden buffer */
  119.             strcpy(hookdata1, (STRPTR)initialstring);
  120.  
  121.             /* re-initialize real/visible string with password chars */
  122.             memset((void *)initialstring, PASSWORDCHAR, strlen((STRPTR)initialstring));
  123.  
  124.             /* Create the window object.
  125.              */
  126.             objects[OID_MAIN] = WindowObject,
  127.                 WA_ScreenTitle, "ClassAct Release 2.0",
  128.                 WA_Title, "ClassAct String Example",
  129.                 WA_Activate, TRUE,
  130.                 WA_DepthGadget, TRUE,
  131.                 WA_DragBar, TRUE,
  132.                 WA_CloseGadget, TRUE,
  133.                 WA_SizeGadget, TRUE,
  134.                 WINDOW_IconifyGadget, TRUE,
  135.                 WINDOW_IconTitle, "String",
  136.                 WINDOW_AppPort, AppPort,
  137.                 WINDOW_Position, WPOS_CENTERMOUSE,
  138.                 WINDOW_ParentGroup, gadgets[GID_MAIN] = VGroupObject,
  139.                     LAYOUT_SpaceOuter, TRUE,
  140.                     LAYOUT_DeferLayout, TRUE,
  141.  
  142.                     LAYOUT_AddChild, gadgets[GID_STRING1] = StringObject,
  143.                         GA_ID, GID_STRING1,
  144.                         GA_RelVerify, TRUE,
  145.                         GA_TabCycle, TRUE,
  146.                         STRINGA_MinVisible, 10,
  147.                         STRINGA_MaxChars, SMAX,
  148.                     StringEnd,
  149.                     CHILD_NominalSize, TRUE,
  150.                     CHILD_Label, LabelObject, LABEL_Text, "String _1", LabelEnd,
  151.  
  152.                     LAYOUT_AddChild, gadgets[GID_STRING2] = StringObject,
  153.                         GA_ID, GID_STRING2,
  154.                         GA_RelVerify, TRUE,
  155.                         GA_TabCycle, TRUE,
  156.                         STRINGA_MinVisible, 10,
  157.                         STRINGA_MaxChars, SMAX,
  158.                         STRINGA_EditHook, &edithook1,
  159.                         STRINGA_TextVal, initialstring,
  160.                     StringEnd,
  161.                     CHILD_Label, LabelObject, LABEL_Text, "String _2", LabelEnd,
  162.  
  163.                     LAYOUT_AddChild, ButtonObject,
  164.                         GA_ID, GID_QUIT,
  165.                         GA_RelVerify, TRUE,
  166.                         GA_Text,"_Quit",
  167.                     ButtonEnd,
  168.                     CHILD_WeightedHeight, 0,
  169.  
  170.                 EndGroup,
  171.             EndWindow;
  172.  
  173.               /*  Object creation sucessful?
  174.                */
  175.             if (objects[OID_MAIN])
  176.             {
  177.                 /*  Open the window.
  178.                  */
  179.                 if (windows[WID_MAIN] = (struct Window *) CA_OpenWindow(objects[OID_MAIN]))
  180.                 {
  181.                     ULONG wait, signal, app = (1L << AppPort->mp_SigBit);
  182.                     ULONG done = FALSE;
  183.                     ULONG result;
  184.                     UWORD code;
  185.  
  186.                      /* Obtain the window wait signal mask.
  187.                      */
  188.                     GetAttr(WINDOW_SigMask, objects[OID_MAIN], &signal);
  189.  
  190.                     /* Activate the first string gadget!
  191.                      */
  192.                     ActivateLayoutGadget( gadgets[GID_MAIN], windows[WID_MAIN], NULL, gadgets[GID_STRING1] );
  193.  
  194.                     /* Input Event Loop
  195.                      */
  196.                     while (!done)
  197.                     {
  198.                         wait = Wait( signal | SIGBREAKF_CTRL_C | app );
  199.  
  200.                         if ( wait & SIGBREAKF_CTRL_C )
  201.                         {
  202.                             done = TRUE;
  203.                         }
  204.                         else
  205.                         {
  206.                             while ( (result = CA_HandleInput(objects[OID_MAIN], &code) ) != WMHI_LASTMSG )
  207.                             {
  208.                                 switch (result & WMHI_CLASSMASK)
  209.                                 {
  210.                                     case WMHI_CLOSEWINDOW:
  211.                                         windows[WID_MAIN] = NULL;
  212.                                         done = TRUE;
  213.                                         break;
  214.  
  215.                                     case WMHI_GADGETUP:
  216.                                         switch (result & WMHI_GADGETMASK)
  217.                                         {
  218.                                             case GID_STRING1:
  219.                                                 printf( "Contents: %s\n", ((struct StringInfo *)(gadgets[GID_STRING1]->SpecialInfo))->Buffer);
  220.  
  221.                                                 break;
  222.  
  223.                                             case GID_STRING2:
  224.                                                 printf( "Contents: %s\n", hookdata1 );
  225.                                                 break;
  226.  
  227.                                             case GID_QUIT:
  228.                                                 done = TRUE;
  229.                                                 break;
  230.                                         }
  231.                                         break;
  232.  
  233.                                     case WMHI_ICONIFY:
  234.                                         CA_Iconify(objects[OID_MAIN]);
  235.                                         windows[WID_MAIN] = NULL;
  236.                                         break;
  237.  
  238.                                     case WMHI_UNICONIFY:
  239.                                         windows[WID_MAIN] = (struct Window *) CA_OpenWindow(objects[OID_MAIN]);
  240.  
  241.                                         if (windows[WID_MAIN])
  242.                                         {
  243.                                             GetAttr(WINDOW_SigMask, objects[OID_MAIN], &signal);
  244.                                         }
  245.                                         else
  246.                                         {
  247.                                             done = TRUE;    // error re-opening window!
  248.                                         }
  249.                                          break;
  250.                                 }
  251.                             }
  252.                         }
  253.                     }
  254.                 }
  255.  
  256.                 /* Disposing of the window object will also close the window if it is
  257.                  * already opened, and it will dispose of the layout object attached to it.
  258.                  */
  259.                 DisposeObject(objects[OID_MAIN]);
  260.             }
  261.  
  262.             /* free the password hook buffer
  263.              */
  264.             FreeVec(hookdata1);
  265.         }
  266.  
  267.         DeleteMsgPort(AppPort);
  268.     }
  269.  
  270.     return(0);
  271. }
  272.  
  273.  
  274.  
  275. /** Password Entry Hook
  276.  **/
  277.  
  278. ULONG __saveds __asm PasswordHook(register __a0 struct Hook *hook, register __a2 struct SGWork *sgw, register __a1 ULONG *msg)
  279. {
  280.     STRPTR pass_ptr = (STRPTR)hook->h_Data;
  281.  
  282.     sgw->BufferPos = sgw->NumChars;
  283.  
  284.     if(*msg == SGH_KEY)
  285.     {
  286.         switch (sgw->EditOp)
  287.         {
  288.             case EO_INSERTCHAR:
  289.                 if(pass_ptr)
  290.                 {
  291.                     pass_ptr[sgw->BufferPos - 1] = sgw->WorkBuffer[sgw->BufferPos - 1];
  292.                     pass_ptr[sgw->BufferPos] = '\0';
  293.                 }
  294.                 sgw->WorkBuffer[sgw->BufferPos - 1] = (UBYTE)PASSWORDCHAR;
  295.                 break;
  296.  
  297.             case EO_DELBACKWARD:
  298.                 if(pass_ptr)
  299.                 {
  300.                     pass_ptr[sgw->BufferPos] = '\0';
  301.                 }
  302.                 break;
  303.  
  304.             default:
  305.                 sgw->Actions &= ~SGA_USE;
  306.                 break;
  307.         }
  308.  
  309.         sgw->Actions |= SGA_REDISPLAY;
  310.         return (~0L);
  311.     }
  312.     if(*msg == SGH_CLICK)
  313.     {
  314.         sgw->BufferPos = sgw->NumChars;
  315.         return (~0L);
  316.     }
  317.     return(0L);
  318. }
  319.